package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.AppointStatusEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;


    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Autowired
    private IAppointmentService appointmentService;
    @Override
    public Consumption save(String appointAno) {

        //根据流水号来查询预约单
        Appointment appointment = appointmentService.selectByAno(appointAno);
        //将预约单状态改成消费中
        appointmentService.changeStatus(appointment.getId(), AppointStatusEnum.CONSUME.getValue());
        //设置结算单的状态为待结算状态
        Consumption consumption = new Consumption();
        //设置预约单流水号信息
        consumption.setAppointmentAno(appointAno);
        //设置门店信息
        consumption.setBusiness(appointment.getBusiness());
        consumption.setStatus(ConsumptionStatusEnum.CONSUME.getValue());
        Date d = new Date();
        //往结算单中添加预约单的流水号
        String date = DateUtil.format(d, "yyyyMMddHHmmss");
        String nums = RandomUtil.randomNumbers(5);
        consumption.setCno(date+nums);
        //从预约单中获取客户名称,联系方式设置到结算单中
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        //进店时间设置为当前时间
        consumption.setCheckinTime(d);
        //创建时间设置成当前时间
        consumption.setCreateTime(d);
        //创建人设置为当前登录用户
        consumption.setCreateUser(UserContext.getCurrentUser());
        //将数据保存到数据库中
        consumptionMapper.insert(consumption);
        return consumption;
    }

    @Override
    public void consume(Long id) {
        //获取当前结算对象
        Consumption consumption = consumptionMapper.selectByPrimaryKey(id);
        //设置状态为待审核状态
        consumption.setStatus(ConsumptionStatusEnum.AUDIT.getValue());
        //设置审核人
        consumption.setPayee(UserContext.getCurrentUser());
        //设置审核时间
        consumption.setPayTime(new Date());
        consumptionMapper.consume(consumption);
    }
}
